#include "widget.h"
#include "ui_widget.h"
#include <QRect>
#include <QShortcut>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->horizontalSlider->setMinimum(400);
    ui->horizontalSlider->setMaximum(1600);
    //按下方向键的时候改变的步长
    ui->horizontalSlider->setSingleStep(100);
    ui->horizontalSlider->setValue(800);

    ui->verticalSlider->setMinimum(300);
    ui->verticalSlider->setMaximum(900);
    //按下方向键的时候改变的步长
    ui->verticalSlider->setSingleStep(100);
    ui->verticalSlider->setValue(600);
    //方向进行翻转,使用倒转
    ui->verticalSlider->setInvertedAppearance(true);

    this->setGeometry(200, 100, 800, 600);

    //设置快捷键
    QShortcut* shortCut1 = new QShortcut(this);
    shortCut1->setKey(QKeySequence("-"));
    connect(shortCut1, &QShortcut::activated, this, &Widget::subValue);

    QShortcut* shortCut2 = new QShortcut(this);
    shortCut2->setKey(QKeySequence("+"));
    connect(shortCut2, &QShortcut::activated, this, &Widget::addValue);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_horizontalSlider_valueChanged(int value)
{
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), value, rect.height());
    ui->label->setText(QString("当前长为: ") + QString::number(value) + QString("宽为: " + QString::number(rect.height())));
}

void Widget::on_verticalSlider_valueChanged(int value)
{
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), rect.width(), value);
    ui->label->setText(QString("当前长为: ") + QString::number(rect.width()) + QString("宽为: " + QString::number(value)));
}

void Widget::subValue()
{
    int value = ui->horizontalSlider->value();
    if (value <= ui->horizontalSlider->minimum()) {
        return;
    }
    //记得及时更新slider的数值,不然会一直使用以前的数值,产生bug
    ui->horizontalSlider->setValue(value - 100);
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), value - 100, rect.height());
    ui->label->setText(QString("当前长为: ") + QString::number(value) + QString("宽为: " + QString::number(rect.height())));
}

void Widget::addValue()
{

    int value = ui->horizontalSlider->value();
    if (value >= ui->horizontalSlider->maximum()) {
        return;
    }

    ui->horizontalSlider->setValue(value + 100);
    const QRect& rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), value + 100, rect.height());
    ui->label->setText(QString("当前长为: ") + QString::number(value) + QString("宽为: " + QString::number(rect.height())));
}
